﻿using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Sustainalytics.Controversies.Service.Util;
using Sustainalytics.Utils.WebApi.ErrorCodes;
using System.Web.Http.Cors;
using Sustainalytics.Controversies.Service.BlInvoker;
using Sustainalytics.Utils.Serilog;

namespace Sustainalytics.Controversies.Service.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    //[ClaimsChecker("api/v1/company/controversies")]
    [RoutePrefix("api/v1/company")]
    public class ControversyGroupAssesmentController: ApiController
    {
        private readonly IServiceBlInvoker _blInvoker;

        public ControversyGroupAssesmentController(IServiceBlInvoker blInvoker)
        {
            _blInvoker = blInvoker;
        }


        [Route("{companyId}/controversyIndicator/{controversyindicatorId}/comparisonGroupControversies")]
        public HttpResponseMessage Get(Guid companyId, Guid controversyindicatorId,[FromUri] ControversyGroupAssesment level)
        {
            try
            {
                ControversiesServiceSerilog.Log.GetcomparisonGroupControversiesSuccess(companyId, controversyindicatorId);
                return
                    Request.CreateResponse(HttpStatusCode.OK, _blInvoker.GetDistributionOfControversyGraphData(companyId,
                        controversyindicatorId, level));
            }
            catch (Exception ex)//todo stelian:  no logging  for exceptions - we need to fix that !
            {
                ControversiesServiceSerilog.Log.GetcomparisonGroupControversiesError(companyId, controversyindicatorId, ex);
                return Request.CreateResponse(HttpStatusCode.InternalServerError,
                    new { ErrorStatusCode = WebAPIGeneralErrorCodes.UnhandledException });
            }
        }
    }
}
